<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '200px'
    },
    extremedata: {
      type: Array,
      default: () => {}
    }
  },
  data() {
    return {
      chart: null,
      desc: [],
      online: [],
      sum: []
    }
  },
  watch: {
    extremedata: {
      handler(newVal, oldVal) {
        console.log('数据接进来啦', newVal)
        this.sum = []
        this.online = []
        this.desc = []
        for (const key in newVal) {
          this.sum.push(newVal[key].sum)
          this.desc.push(newVal[key].desc)
          this.online.push(newVal[key].onLine)
        }
        this.initChart() // 刷新图表
      },
      deep: true
    }
  },
  mounted() {
    // this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        angleAxis: {
          data: this.desc,
          axisLabel: {
            show: true,
            textStyle: {
              color: '#c3dbff', // 更改坐标轴文字颜色
              fontSize: 14 // 更改坐标轴文字大小
            }
          }
        },
        radiusAxis: {
          axisLine: {
            // 坐标轴轴线设置
            show: true,
            lineStyle: {
              color: '#fff'
            }
          },
          axisTick: {
            // 坐标轴刻度设置
            show: true
          },
          axisLabel: {
            // 刻度标签设置
            margin: 2, // 刻度与坐标轴之间的距离
            textStyle: {
              color: '#fff'
            }
          }
        },
        polar: {
          center: ['50%', '50%']
        },
        legend: {
          left: '75%',
          orient: 'vertical',
          y: '15%',
          itemWidth: 5,
          itemHeight: 5,
          show: true,
          data: ['在线', '离线'],
          textStyle: {
            // 图例文字的样式
            color: ['#669999', '#FFCC66'],
            fontSize: 16
          }
        },
        color: ['#669999', '#FFCC66'],
        series: [
          {
            type: 'bar',
            coordinateSystem: 'polar',
            name: '在线',
            stack: 'a',
            data: this.online
          },
          {
            type: 'bar',
            data: this.sum - this.online,
            coordinateSystem: 'polar',
            name: '离线',
            stack: 'a'
          }
        ]
      })
    }
  }
}
</script>
